home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Demo / tkinter / www / Para.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  16.4 KB  |  414 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. Int = type(0)
  5.  
  6. class Para:
  7.     
  8.     def __init__(self):
  9.         self.words = []
  10.         self.just = 'l'
  11.         self.indent_left = self.indent_right = self.indent_hang = 0
  12.         self.left = self.top = self.right = self.bottom = self.width = self.height = self.lines = None
  13.  
  14.     
  15.     def addword(self, d, font, text, space, stretch):
  16.         if font != None:
  17.             d.setfont(font)
  18.         
  19.         width = d.textwidth(text)
  20.         ascent = d.baseline()
  21.         descent = d.lineheight() - ascent
  22.         spw = d.textwidth(' ')
  23.         space = space * spw
  24.         stretch = stretch * spw
  25.         tuple = (font, text, width, space, stretch, ascent, descent)
  26.         self.words.append(tuple)
  27.  
  28.     
  29.     def bgn_anchor(self, id):
  30.         self.words.append(id)
  31.  
  32.     
  33.     def end_anchor(self, id):
  34.         self.words.append(0)
  35.  
  36.     
  37.     def getlength(self):
  38.         total = 0
  39.         for word in self.words:
  40.             pass
  41.         
  42.         return total
  43.  
  44.     
  45.     def tabto(self, tab):
  46.         total = 0
  47.         (as, de) = (1, 0)
  48.         for i in range(len(self.words)):
  49.             word = self.words[i]
  50.             (fo, te, wi, sp, st, as, de) = word
  51.             self.words[i] = (fo, te, wi, sp, 0, as, de)
  52.             total = total + wi + sp
  53.         
  54.         if total < tab:
  55.             self.words.append((None, '', 0, tab - total, 0, as, de))
  56.         
  57.  
  58.     
  59.     def makehangingtag(self, hang):
  60.         self.tabto(hang)
  61.         self.indent_left = self.indent_left + hang
  62.         self.indent_hang = -hang
  63.  
  64.     
  65.     def layout(self, linewidth):
  66.         self.width = linewidth
  67.         height = 0
  68.         self.lines = lines = []
  69.         avail1 = self.width - self.indent_left - self.indent_right
  70.         avail = avail1 - self.indent_hang
  71.         words = self.words
  72.         i = 0
  73.         n = len(words)
  74.         lastfont = None
  75.         while i < n:
  76.             firstfont = lastfont
  77.             charcount = 0
  78.             width = 0
  79.             stretch = 0
  80.             ascent = 0
  81.             descent = 0
  82.             lsp = 0
  83.             j = i
  84.             while i < n:
  85.                 word = words[i]
  86.                 if type(word) == Int:
  87.                     if word > 0 and width >= avail:
  88.                         break
  89.                     
  90.                     i = i + 1
  91.                     continue
  92.                 
  93.                 (fo, te, wi, sp, st, as, de) = word
  94.                 if width + wi > avail and width > 0 and wi > 0:
  95.                     break
  96.                 
  97.                 if fo != None:
  98.                     lastfont = fo
  99.                     if width == 0:
  100.                         firstfont = fo
  101.                     
  102.                 
  103.                 charcount = charcount + len(te) + (sp > 0)
  104.                 width = width + wi + sp
  105.                 lsp = sp
  106.                 stretch = stretch + st
  107.                 lst = st
  108.                 ascent = max(ascent, as)
  109.                 descent = max(descent, de)
  110.                 i = i + 1
  111.             while i > j and type(words[i - 1]) == Int and words[i - 1] > 0:
  112.                 i = i - 1
  113.             width = width - lsp
  114.             if i < n:
  115.                 stretch = stretch - lst
  116.             else:
  117.                 stretch = 0
  118.             tuple = (i - j, firstfont, charcount, width, stretch, ascent, descent)
  119.             lines.append(tuple)
  120.             height = height + ascent + descent
  121.             avail = avail1
  122.         self.height = height
  123.  
  124.     
  125.     def visit(self, wordfunc, anchorfunc):
  126.         avail1 = self.width - self.indent_left - self.indent_right
  127.         avail = avail1 - self.indent_hang
  128.         v = self.top
  129.         i = 0
  130.         for tuple in self.lines:
  131.             (wordcount, firstfont, charcount, width, stretch, ascent, descent) = tuple
  132.             h = self.left + self.indent_left
  133.             extra = 0
  134.             if self.just == 'r':
  135.                 h = h + avail - width
  136.             elif self.just == 'c':
  137.                 h = h + (avail - width) / 2
  138.             elif self.just == 'lr' and stretch > 0:
  139.                 extra = avail - width
  140.             
  141.             v2 = v + ascent + descent
  142.             for j in range(i, i + wordcount):
  143.                 word = self.words[j]
  144.                 if type(word) == Int:
  145.                     ok = anchorfunc(self, tuple, word, h, v)
  146.                     continue
  147.                 
  148.                 (fo, te, wi, sp, st, as, de) = word
  149.                 if extra > 0 and stretch > 0:
  150.                     ex = extra * st / stretch
  151.                     extra = extra - ex
  152.                     stretch = stretch - st
  153.                 else:
  154.                     ex = 0
  155.                 h2 = h + wi + sp + ex
  156.                 ok = wordfunc(self, tuple, word, h, v, h2, v2, j == i, j == i + wordcount - 1)
  157.                 if ok != None:
  158.                     return ok
  159.                 
  160.                 h = h2
  161.             
  162.             v = v2
  163.             i = i + wordcount
  164.             avail = avail1
  165.         
  166.  
  167.     
  168.     def render(self, d, left, top, right):
  169.         if self.width != right - left:
  170.             self.layout(right - left)
  171.         
  172.         self.left = left
  173.         self.top = top
  174.         self.right = right
  175.         self.bottom = self.top + self.height
  176.         self.anchorid = 0
  177.         
  178.         try:
  179.             self.d = d
  180.             self.visit(self.__class__._renderword, self.__class__._renderanchor)
  181.         finally:
  182.             self.d = None
  183.  
  184.         return self.bottom
  185.  
  186.     
  187.     def _renderword(self, tuple, word, h, v, h2, v2, isfirst, islast):
  188.         if word[0] != None:
  189.             self.d.setfont(word[0])
  190.         
  191.         baseline = v + tuple[5]
  192.         self.d.text((h, baseline - word[5]), word[1])
  193.         if self.anchorid > 0:
  194.             self.d.line((h, baseline + 2), (h2, baseline + 2))
  195.         
  196.  
  197.     
  198.     def _renderanchor(self, tuple, word, h, v):
  199.         self.anchorid = word
  200.  
  201.     
  202.     def hitcheck(self, mouseh, mousev):
  203.         self.mouseh = mouseh
  204.         self.mousev = mousev
  205.         self.anchorid = 0
  206.         self.hits = []
  207.         self.visit(self.__class__._hitcheckword, self.__class__._hitcheckanchor)
  208.         return self.hits
  209.  
  210.     
  211.     def _hitcheckword(self, tuple, word, h, v, h2, v2, isfirst, islast):
  212.         if self.anchorid > 0:
  213.             if self.mouseh <= self.mouseh:
  214.                 pass
  215.             elif self.mouseh <= h2:
  216.                 if self.mousev <= self.mousev:
  217.                     pass
  218.                 elif self.mousev <= v2:
  219.                     self.hits.append(self.anchorid)
  220.                 
  221.  
  222.     
  223.     def _hitcheckanchor(self, tuple, word, h, v):
  224.         self.anchorid = word
  225.  
  226.     
  227.     def hasanchor(self, id):
  228.         if not id in self.words:
  229.             pass
  230.         return -id in self.words
  231.  
  232.     
  233.     def extract(self):
  234.         text = ''
  235.         for w in self.words:
  236.             if type(w) != Int:
  237.                 word = w[1]
  238.                 text = text + word
  239.             
  240.         
  241.         return text + '\n'
  242.  
  243.     
  244.     def whereis(self, d, mouseh, mousev):
  245.         if mousev < self.top or mousev > self.bottom:
  246.             return None
  247.         
  248.         self.mouseh = mouseh
  249.         self.mousev = mousev
  250.         self.lastfont = None
  251.         self.charcount = 0
  252.         
  253.         try:
  254.             self.d = d
  255.             return self.visit(self.__class__._whereisword, self.__class__._whereisanchor)
  256.         finally:
  257.             self.d = None
  258.  
  259.  
  260.     
  261.     def _whereisword(self, tuple, word, h1, v1, h2, v2, isfirst, islast):
  262.         (fo, te, wi, sp, st, as, de) = word
  263.         if fo != None:
  264.             self.lastfont = fo
  265.         
  266.         h = h1
  267.         if isfirst:
  268.             h1 = 0
  269.         
  270.         if islast:
  271.             h2 = 999999
  272.         
  273.         if self.mousev <= self.mousev:
  274.             pass
  275.         elif self.mousev <= v2:
  276.             pass
  277.         if not None if self.mouseh <= self.mouseh else self.mouseh <= h2:
  278.             self.charcount = self.charcount + len(te) + (sp > 0)
  279.             return None
  280.         
  281.         if self.lastfont != None:
  282.             self.d.setfont(self.lastfont)
  283.         
  284.         cc = 0
  285.         for c in te:
  286.             cw = self.d.textwidth(c)
  287.             cc = cc + 1
  288.             h = h + cw
  289.         
  290.         self.charcount = self.charcount + cc
  291.         if self.mouseh <= (h + h2) / 2:
  292.             return self.charcount
  293.         else:
  294.             return self.charcount + 1
  295.  
  296.     
  297.     def _whereisanchor(self, tuple, word, h, v):
  298.         pass
  299.  
  300.     
  301.     def screenpos(self, d, pos):
  302.         if pos < 0:
  303.             (ascent, descent) = self.lines[0][5:7]
  304.             return (self.left, self.top, self.top + ascent, self.top + ascent + descent)
  305.         
  306.         self.pos = pos
  307.         self.lastfont = None
  308.         
  309.         try:
  310.             self.d = d
  311.             ok = self.visit(self.__class__._screenposword, self.__class__._screenposanchor)
  312.         finally:
  313.             self.d = None
  314.  
  315.         if ok == None:
  316.             (ascent, descent) = self.lines[-1][5:7]
  317.             ok = (self.right, self.bottom - ascent - descent, self.bottom - descent, self.bottom)
  318.         
  319.         return ok
  320.  
  321.     
  322.     def _screenposword(self, tuple, word, h1, v1, h2, v2, isfirst, islast):
  323.         (fo, te, wi, sp, st, as, de) = word
  324.         if fo != None:
  325.             self.lastfont = fo
  326.         
  327.         cc = len(te) + (sp > 0)
  328.         if self.pos > cc:
  329.             self.pos = self.pos - cc
  330.             return None
  331.         
  332.         if self.pos < cc:
  333.             self.d.setfont(self.lastfont)
  334.             h = h1 + self.d.textwidth(te[:self.pos])
  335.         else:
  336.             h = h2
  337.         (ascent, descent) = tuple[5:7]
  338.         return (h, v1, v1 + ascent, v2)
  339.  
  340.     
  341.     def _screenposanchor(self, tuple, word, h, v):
  342.         pass
  343.  
  344.     
  345.     def invert(self, d, pos1, pos2):
  346.         if pos1 == None:
  347.             pos1 = (self.left, self.top, self.top, self.top)
  348.         else:
  349.             pos1 = self.screenpos(d, pos1)
  350.         if pos2 == None:
  351.             pos2 = (self.right, self.bottom, self.bottom, self.bottom)
  352.         else:
  353.             pos2 = self.screenpos(d, pos2)
  354.         (h1, top1, baseline1, bottom1) = pos1
  355.         (h2, top2, baseline2, bottom2) = pos2
  356.         if bottom1 <= top2:
  357.             d.invert((h1, top1), (self.right, bottom1))
  358.             h1 = self.left
  359.             if bottom1 < top2:
  360.                 d.invert((h1, bottom1), (self.right, top2))
  361.             
  362.             (top1, bottom1) = (top2, bottom2)
  363.         
  364.         d.invert((h1, top1), (h2, bottom2))
  365.  
  366.  
  367.  
  368. def test():
  369.     import stdwin
  370.     *
  371.     words = ('The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.')
  372.     paralist = []
  373.     for just in ('l', 'r', 'lr', 'c'):
  374.         p = Para()
  375.         p.just = just
  376.         p.addword(stdwin, ('New York', 'p', 12), words[0], 1, 1)
  377.         for word in words[1:-1]:
  378.             p.addword(stdwin, None, word, 1, 1)
  379.         
  380.         p.addword(stdwin, None, words[-1], 2, 4)
  381.         p.addword(stdwin, ('New York', 'b', 18), 'Bye!', 0, 0)
  382.         p.addword(stdwin, ('New York', 'p', 10), 'Bye!', 0, 0)
  383.         paralist.append(p)
  384.     
  385.     window = stdwin.open('Para.test()')
  386.     while 1:
  387.         (etype, win, detail) = stdwin.getevent()
  388.         if etype == WE_MOUSE_DOWN:
  389.             if selpara and start != None and stop != None:
  390.                 d = window.begindrawing()
  391.                 selpara.invert(d, start, stop)
  392.                 d.close()
  393.             
  394.             start = stop = selpara = None
  395.             (mouseh, mousev) = detail[0]
  396.             for p in paralist:
  397.                 start = p.whereis(stdwin, mouseh, mousev)
  398.             
  399.         
  400.         if etype == WE_MOUSE_UP and start != None and selpara:
  401.             (mouseh, mousev) = detail[0]
  402.             stop = selpara.whereis(stdwin, mouseh, mousev)
  403.             if stop == None:
  404.                 start = selpara = None
  405.             elif start > stop:
  406.                 (start, stop) = (stop, start)
  407.             
  408.             d = window.begindrawing()
  409.             selpara.invert(d, start, stop)
  410.             d.close()
  411.         
  412.     window.close()
  413.  
  414.